package com.serialcommunication.gui.component;

import static com.serialcommunication.constants.GuiConfiguration.PANEL_LED_SIZE_3;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JLabel;

import com.serialcommunication.enums.Engine;
import com.serialcommunication.gui.frame.MainFrame;
import com.serialcommunication.interfaces.IAction;
import com.serialcommunication.interfaces.IBaseComponent;
import com.serialcommunication.interfaces.INotifyComponent;
import com.serialcommunication.interfaces.ITransmitComponent;

public class DisplayComponent extends BaseComponent implements INotifyComponent {
	
	private static final Color colorOk = Color.GREEN;
	private static final Color colorNotOk = Color.RED;
	
	private String value;
	private JLabel label;

	public DisplayComponent(Engine pEngine, IAction pAction, MainFrame pFrame, int pSizeLevel) {
		super(pEngine, pAction, pFrame);
		
		label = new JLabel("sdf");
		label.setOpaque(true);
		label.setFont(new Font(Font.DIALOG, Font.PLAIN, 11));
		setColor(colorNotOk);
		addComponentsToPanel();
		setSizeOfElements(pSizeLevel);
		alignElementsWithinPanel();
		setValue("16.43");
	}
	
	public DisplayComponent(Engine pEngine, IAction pAction, MainFrame pFrame, Color pColor, int pSizeLevel) {
		super(pEngine, pAction, pFrame);
		
		label = new JLabel("sdf");
		label.setOpaque(true);
		label.setFont(new Font(Font.DIALOG, Font.PLAIN, 11));
		setColor(pColor);
		addComponentsToPanel();
		setSizeOfElements(pSizeLevel);
		alignElementsWithinPanel();
		setValue("16.43");
	}
	
	public void alignElementsWithinPanel() {
		label.setLocation(0, 0);
	}
	
	public void setSizeOfElements(int pSizeLevel) {
		label.setSize(PANEL_LED_SIZE_3);
		getPanel().setSize(PANEL_LED_SIZE_3);
	}
	
	public void addComponentsToPanel() {
		getPanel().add(label);
	}

	@Override
	public void setValue(String pValue) {
		label.setText(getAction().getTitle() + ": " + pValue);
	}

	@Override
	public void setColor(Color color) {
		label.setBackground(color);
		getPanel().setBackground(color);
	}

	@Override
	public void setColorPositive() {
		label.setBackground(colorOk);
		getPanel().setBackground(colorOk);
	}

	@Override
	public void setColorNegative() {
		label.setBackground(colorNotOk);
		getPanel().setBackground(colorNotOk);
	}
}
